import { constantRoutes, dynamicRouter } from '@/router'
const state = {
  // 给所有用户一个默认的静态路由
  routes: constantRoutes
}
const mutations = {
  // 更新路由
  updateRoutes(state, route) {
    // 路由更新的基础是基于静态路由
    state.routes = [...constantRoutes, ...route]
  }
}
const actions = {
  // 获取权限路由，用户登录时，将获取到的用户的权限数据进行传送进来获得相应的权限路由
  filterRoute(context, menuArr) {
    const routes = []
    menuArr.forEach(item => {
      const filterRoutes = dynamicRouter.filter(obj => obj.children[0].name === item)
      routes.push(...filterRoutes)
    })
    context.commit('updateRoutes', routes)
    return routes
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
